{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们使用最小二乘法，求解每个方程的系数，并通过逐渐增加阶数，为模型定阶"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>P</th>\n",
       "      <th>AIC</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>13.580156</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>13.312225</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>13.543633</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>14.266087</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>15.512437</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>17.539047</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7</td>\n",
       "      <td>20.457337</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>8</td>\n",
       "      <td>24.385459</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>9</td>\n",
       "      <td>29.438091</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>10</td>\n",
       "      <td>35.785909</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    P        AIC\n",
       "0   1  13.580156\n",
       "1   2  13.312225\n",
       "2   3  13.543633\n",
       "3   4  14.266087\n",
       "4   5  15.512437\n",
       "5   6  17.539047\n",
       "6   7  20.457337\n",
       "7   8  24.385459\n",
       "8   9  29.438091\n",
       "9  10  35.785909"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pickle\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "with open('data/subdata_diff1.pkl', 'rb') as f:\n",
    "    subdata_diff1 = pickle.load(f)\n",
    "    \n",
    "# 模型阶数从1开始逐一增加\n",
    "rows, cols = subdata_diff1.shape\n",
    "aicList = []\n",
    "lmList = []\n",
    "\n",
    "for p in range(1,11):\n",
    "    baseData = None\n",
    "    for i in range(p,rows):\n",
    "        tmp_list = list(subdata_diff1[i,:]) + list(subdata_diff1[i-p:i].flatten())\n",
    "        if baseData is None:\n",
    "            baseData = [tmp_list]\n",
    "        else:\n",
    "            baseData = np.r_[baseData, [tmp_list]]\n",
    "    X = np.c_[[1]*baseData.shape[0],baseData[:,cols:]]\n",
    "    Y = baseData[:,0:cols]\n",
    "    coefMatrix = np.matmul(np.matmul(np.linalg.inv(np.matmul(X.T,X)),X.T),Y)\n",
    "    aic = np.log(np.linalg.det(np.cov(Y - np.matmul(X,coefMatrix),rowvar=False))) + 2*(coefMatrix.shape[0]-1)**2*p/baseData.shape[0]\n",
    "    aicList.append(aic)\n",
    "    lmList.append(coefMatrix)\n",
    "    \n",
    "\n",
    "with open('data/aicList.pkl', 'wb') as f:\n",
    "    pickle.dump(aicList, f)\n",
    "    \n",
    "with open('data/lmList.pkl', 'wb') as f:\n",
    "    pickle.dump(lmList, f)\n",
    "    \n",
    "#对比查看阶数和AIC\n",
    "pd.DataFrame({\"P\":range(1,11),\"AIC\":aicList})"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
